package 二叉树.前序遍历.递归方法;


import java.util.ArrayList;
import java.util.List;

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }

}

public class Solution {

    public int[] preorderTraversal (TreeNode root) {
        // write code here
        ArrayList<Integer> list = new ArrayList<>();
        PreOrder(list,root);
        int[] ints = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            ints[i]=list.get(i);
        }
        return ints;
    }

    // 前序遍历方法: 递归
    public void PreOrder(List<Integer> list,TreeNode root){
        if (root!=null){
            list.add(root.val);
            PreOrder(list,root.left);
            PreOrder(list,root.right);
        }else {
            // 如果是空节点,直接返回
            return;
        }
    }
}
